# -*- coding: utf-8 -*-
import hashlib
from datetime import datetime
import sqlite3
import requests

# ##########################################
# 主程序开始
# ##########################################
# open.nbeport.gov.cn
api_host='api.nbeport.gov.cn'
# open.trainer.nbeport.com
# api_host='api.trainer.nbeport.com'
api_url='http://%s/router/rest'%api_host
user_id='devhd01'
app_secret='5127f00dcf1c449e9fc59879e780ae41'

entryId='290620130063702618'
billNo='MOLU11020558664'
# sign=4371e3dc5319dec2fbbdfacdafe3c6e6&user_id=devhd01&timestamp=2013-06-28+10%3A19%3A42&format=xml&method=cxsoft.billdetail&entryId=290620130063702618&apprNo=&billNo=MOLU11020558664

# headers = {"User-Agent": "Java/1.6.0_18","Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2","Connection":"keep-alive"}



def cxsoft_billdetail(entryId,billNo):
    nowdate=datetime.now()
    timestamp=nowdate.strftime('%Y-%m-%d %H:%M:%S')
    print timestamp
    sign_str='%s%s%s'%(user_id,app_secret,timestamp)
    print sign_str
    sign=hashlib.new('md5',sign_str).hexdigest()
    print sign

    params = {
        'sign': sign,
        'user_id': user_id,
        'timestamp': timestamp,
        'format': 'xml',
        'method': 'cxsoft.billdetail',
        'entryId': entryId,
        'billNo': billNo
    }

    r = requests.get(api_url, params=params)
    print r.text

# 这个是api.trainer.nbeport.com的接口
def cxsoft_entryflow(entryId,billNo):
    nowdate=datetime.now()
    timestamp=nowdate.strftime('%Y-%m-%d %H:%M:%S')
    print timestamp
    sign_str='%s%s%s'%(user_id,app_secret,timestamp)
    print sign_str
    sign=hashlib.new('md5',sign_str).hexdigest()
    print sign

    params = {
        'sign': sign,
        'user_id': user_id,
        'timestamp': timestamp,
        'format': 'xml',
        'method': 'cxsoft.entryflow',
        'entryId': entryId,
        'billNo': billNo
    }

    r = requests.get(api_url, params=params)
    print r.text


def get_data():
    conn=sqlite3.connect(r'e:\web\qp4\qp.db3')
    conn.text_factory = str
    conn.row_factory = sqlite3.Row

    datas=conn.execute('select entry_id,bill_no from dec_mess_status').fetchall()
    conn.close()
    return datas

for data in get_data():
    cxsoft_entryflow(data[0],data[1])

